﻿using ff14bot;
using ff14bot.Managers;
using ff14bot.Objects;
using System.Collections.Generic;
using System.Linq;

namespace MoreDots
{
    public static class Helpers
    {
        public static bool HasAura(this GameObject unit, string spellname, bool isMyAura = false, int msLeft = 0)
        {
            var unitasc = (unit as Character);
            if (unit == null || unitasc == null)
            {
                return false;
            }
            IEnumerable<Aura> auras;
            auras = isMyAura ? unitasc.CharacterAuras.Where(r => r.CasterId == Core.Player.ObjectId && r.Name == spellname) : unitasc.CharacterAuras.Where(r => r.Name == spellname);

            var retval = auras.Any(aura => aura.TimespanLeft.TotalMilliseconds > msLeft);

            return retval;
        }

        public static bool HasAura(this GameObject unit, string[] spells, bool isMyAura = false, int msLeft = 0, bool hasAll = true)
        {
            bool retval = false;
            var unitasc = (unit as Character);
            if (unit == null || unitasc == null)
            {
                return false;
            }
            foreach (var spellname in spells)
            {
                IEnumerable<Aura> auras;
                auras = isMyAura ? unitasc.CharacterAuras.Where(r => r.CasterId == Core.Player.ObjectId && r.Name == spellname) : unitasc.CharacterAuras.Where(r => r.Name == spellname);
                retval = auras.Any(aura => aura.TimespanLeft.TotalMilliseconds > msLeft);
                if (hasAll && !retval)
                {
                    return retval;
                }
            }
            return retval;
        }

        public static IEnumerable<BattleCharacter> UnfriendlyUnits
        {
            get
            {
                return
                    GameObjectManager.GetObjectsOfType<BattleCharacter>().Where(u => !u.IsDead && u.CanAttack);
            }
        }

        public static int EnemiesNearTarget(float range)
        {
            var target = Core.Player.CurrentTarget;
            if (target == null)
                return 0;
            var tarLoc = target.Location;
            return UnfriendlyUnits.Count(u => u.ObjectId != target.ObjectId && u.Location.Distance3D(tarLoc) <= range);
        }
    }
}
